package com.hanbang.taitaigou.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.hanbang.taitaigou.R;
import com.hanbang.taitaigou.pay.wxpay.WxPay;
import com.hanbang.taitaigou.ui.activity.aboutme.myOrder.AboutMyOrderActivity;
import com.hanbang.taitaigou.utils.ui.LogUtils;
import com.tencent.mm.sdk.modelbase.BaseReq;
import com.tencent.mm.sdk.modelbase.BaseResp;
import com.tencent.mm.sdk.modelpay.PayResp;
import com.tencent.mm.sdk.openapi.IWXAPI;
import com.tencent.mm.sdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.sdk.openapi.WXAPIFactory;

import org.xutils.HttpManager;
import org.xutils.x;

/**
 * 微信回调，测试
 */
public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private IWXAPI api;

    public HttpManager httpManager = x.http();



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wxpay_result);
        api = WXAPIFactory.createWXAPI(this, WxPay.appId);
        api.handleIntent(getIntent(), this);
    }


    @Override
    protected void onResume() {
        super.onResume();
        finish();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
    }

    /**
     * resp.errCode：
     * 为0，支付成功，
     * 为-1：失败
     * 为-2：用户取消
     *
     * @param resp
     */
    @Override
    public void onResp(BaseResp resp) {
        PayResp re = (PayResp) resp;
        LogUtils.e("aaa", "++++" + resp.errCode);
        if (resp.errCode == 0) {
            //支付成功，
            //把传过来的字符串转换成数组
            Intent intent = new Intent(WXPayEntryActivity.this, AboutMyOrderActivity.class);
                    startActivity(intent);
                    finish();
//            String[] temp = re.extData.split(",");
//
//            HttpRequestParams httpRequestParams = new HttpRequestParams("UpdateOrderStatus");
//            httpRequestParams.addBodyParameter("mobile", temp[1]);
//            httpRequestParams.addBodyParameter("tp_password", temp[2]);
//            httpRequestParams.addBodyParameter("order_id", temp[0]);
//            httpRequestParams.addBodyParameter("edit_type", "sub_order");
//            httpRequestParams.addBodyParameter("payment_id", "1");
//            httpManager.post(httpRequestParams, new HttpCallBack<String>(this, "") {
//                @Override
//                public void onSuccess(String result) {
//                    super.onSuccess(result);
//                    Intent intent = new Intent(WXPayEntryActivity.this, AboutMyOrderActivity.class);
////                    intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
//                    startActivity(intent);
//                    finish();
//                }
//            });


        } else if(resp.errCode == -2){
            Intent intent = new Intent(this, AboutMyOrderActivity.class);
//            intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
            startActivity(intent);
            finish();
        }

    }


}